package com.hpxx.ylzswl;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.hpxx.ylzswl.activity.LoginActivity;
import com.hpxx.ylzswl.adapter.HoemPagerFragmentAdapter;
import com.hpxx.ylzswl.base.BaseActivity;
import com.hpxx.ylzswl.base.BaseEvent;
import com.hpxx.ylzswl.base.BaseFragment;
import com.hpxx.ylzswl.event.PersonalEvent;
import com.hpxx.ylzswl.event.QuitLoginEvent;
import com.hpxx.ylzswl.fragment.MyFragment;
import com.hpxx.ylzswl.fragment.OrderFragment;
import com.hpxx.ylzswl.utils.ConstantsUtils;
import com.hpxx.ylzswl.views.CustomViewPager;
import com.universal.frame.generalutils.SharedPreferencesUtil;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.TagAliasCallback;
import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;

public class MainActivity extends BaseActivity implements View.OnClickListener{

    private LinearLayout ll_tab_1, ll_tab_2;
    private ImageView iv_img_1, iv_img_2;
    private TextView tv_1, tv_2;
    private CustomViewPager cvp_fragment;
    private List<BaseFragment> fragmentList = new ArrayList<BaseFragment>();
    private HoemPagerFragmentAdapter pagerAdapter;
    //记录用户首次点击返回键的时间
    private long firstTime = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        setSwipeBackEnable(false);
    }

    @Override
    public void initView() {
        cvp_fragment = (CustomViewPager) findViewById(R.id.cvp_fragment);
        initViewpager();
        tabFindById();
        setImage(1);
    }

    private void tabFindById() {
        tv_1 = (TextView) findViewById(R.id.tv_1);
        tv_2 = (TextView) findViewById(R.id.tv_2);


        ll_tab_1 = (LinearLayout) findViewById(R.id.ll_tab_1);
        ll_tab_2 = (LinearLayout) findViewById(R.id.ll_tab_2);


        iv_img_1 = (ImageView) findViewById(R.id.iv_img_1);
        iv_img_2 = (ImageView) findViewById(R.id.iv_img_2);



        ll_tab_1.setOnClickListener(this);
        ll_tab_2.setOnClickListener(this);


    }

    /**
     * 初始化ViewPager
     */
    private void initViewpager() {
        fragmentList.add(OrderFragment.newInstance(0));
        fragmentList.add(MyFragment.newInstance(1));
        pagerAdapter = new HoemPagerFragmentAdapter(getSupportFragmentManager(), this, fragmentList);
        cvp_fragment.setAdapter(pagerAdapter);
        cvp_fragment.setOffscreenPageLimit(fragmentList.size());
        cvp_fragment.setCurrentItem(0, false);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_tab_1:
                //首页
                setImage(1);
                break;
            //我的
            case R.id.ll_tab_2:
                setImage(2);
                EventBus.getDefault().post(new PersonalEvent());
                break;
            default:
                break;
        }
    }

    /**
     * 设置ImageVIew图片切换
     */
    private void setImage(int flg) {
        switch (flg) {
            case 1:
                iv_img_1.setBackgroundResource(R.mipmap.icon_home_true);
                tv_1.setTextColor(getResources().getColor(R.color.bottom_true));
                tv_2.setTextColor(getResources().getColor(R.color.bottom_flase));
                iv_img_2.setBackgroundResource(R.mipmap.icon_my_flase);
                cvp_fragment.setCurrentItem(0, false);
                break;

            case 2:
                iv_img_1.setBackgroundResource(R.mipmap.icon_home_flase);

                tv_1.setTextColor(getResources().getColor(R.color.bottom_flase));
                tv_2.setTextColor(getResources().getColor(R.color.bottom_true));

                iv_img_2.setBackgroundResource(R.mipmap.icon_my_true);

                cvp_fragment.setCurrentItem(1, false);
                break;

            default:
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            if (System.currentTimeMillis() - firstTime > 2000) {
                Toast.makeText(MainActivity.this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
                firstTime = System.currentTimeMillis();
            } else {
                finish();
                System.exit(0);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
    @Override
    @Subscribe
    public void onEventMainThread(BaseEvent event) throws IOException {
        super.onEventMainThread(event);
        if (event instanceof QuitLoginEvent) {
            startActivity(new Intent(this, LoginActivity.class));
            JPushInterface.clearAllNotifications(this);
            JPushInterface.setAlias(getApplicationContext(), //上下文对象
                    "", //别名
                    new TagAliasCallback() {//回调接口,i=0表示成功,其它设置失败
                        @Override
                        public void gotResult(int i, String s, Set<String> set) {
                            Log.e("lzxxxxxxxxxxxxxxxxx", "gotResult: "+i+ s+set);
                            SharedPreferencesUtil.putBoolean(MainActivity.this, ConstantsUtils.IS_ALIAS, true);
                        }
                    });
            finish();
        }
    }


}
